﻿using BLL;
using DevExpress.XtraEditors;
using Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.选择窗体;

namespace 后整理管理系统.新增窗体
{
    public partial class 检测单 : Sunny.UI.UIForm
    {
        private FormUseful Useful;
        private SqlSugarClient _db = Connect.GetSugar();
        private TestData OldDoc = new TestData();
        public 检测单(FormUseful formUseful,TestData doc)
        {
            InitializeComponent();
            this.Useful = formUseful;
            this.OldDoc = doc;
        }

        private void 检测单_Load(object sender, EventArgs e)
        {
            if (Useful == FormUseful.新增) 
            {
                Addnew();
            }
            else
            {
                if (Useful == FormUseful.修改)
                {
                    Edit(OldDoc);
                }
                else
                {
                    if(Useful==FormUseful.复制)
                    {
                        Edit(OldDoc);
                        dateTimePicker1.Value = DateTime.Now;
                        txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.检测单, dateTimePicker1.Value, DanjuLeiXing.检测单);
                    }
                }
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        private void Addnew()
        {
            dateTimePicker1.Value=DateTime.Now;
            txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.检测单, dateTimePicker1.Value , DanjuLeiXing.检测单); 
            txtganghao.Text = string.Empty;
            txtgunxing.Text = string.Empty;
            txtordernum.Text = string.Empty;
            txtboli.Text = string.Empty;
            txtpingming.Text = string.Empty;
            txtshuiya.Text = string.Empty;
            txttoushi.Text = string.Empty;
            txtyanse.Text = string.Empty;
            txtzhibiao.Text = string.Empty;
            txtkehu.Text = string.Empty;
        }
        private void Edit(TestData doc)
        {
            txtdanhao.Text = doc.DocNum;
            txtganghao.Text = doc.LotNo;
            txtpingming.Text = doc.FabricName;
            txtyanse.Text = doc.Color;
            txttoushi.Text = doc.MoisturePermeability;
            txtshuiya.Text = doc.Hydraulic;
            txtzhibiao.Text = doc.Reqiured;
            txtgunxing.Text = doc.RollType;
            txtboli.Text = doc.Divest;
            txtkehu.Text = doc.CustomerName;
            dateTimePicker1.Value = doc.DocumentDate;
            txtordernum.Text = doc.OrderNum;

        }
        private void button2_Click(object sender, EventArgs e)
        {
            var linkman = new LXR() { ZJC = "", MC = "" };
            var fm = new 客户选择() { linkman = linkman };
            fm.ShowDialog();
            txtkehu.Text = fm.linkman.MC;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtkehu.Text))
            {
                AlterDlg.Show("请先输入客户");
                return;
            }
            if (string.IsNullOrEmpty(txtpingming.Text))
            {
                AlterDlg.Show("请先输入品名");
                return;
            }
            if (string.IsNullOrEmpty(txtyanse.Text))
            {
                AlterDlg.Show("请先输入颜色");
                return;
            }
            if (string.IsNullOrEmpty(txtboli.Text)&&string.IsNullOrEmpty(txtshuiya.Text )&&string.IsNullOrEmpty(txtzhibiao.Text )&&string.IsNullOrEmpty(txttoushi.Text ))
            {
                AlterDlg.Show("请输入一个检测结果");
                return;
            }
            try
            {
                if (Useful == FormUseful.新增 || Useful == FormUseful.复制)
                {
                    var doc = new TestData();
                    AssignmentTestDoc(doc);
                    using (var trans = _db.UseTran())
                    {
                        _db.Insertable(doc).ExecuteCommand();
                        _db.CommitTran();
                    }
                }
                else
                {
                    if (Useful == FormUseful.修改)
                    {
                        AssignmentTestDoc(OldDoc);
                        using (var trans = _db.UseTran())
                        {
                            _db.Updateable(OldDoc).ExecuteCommand();
                            _db.CommitTran();
                        }
                    }

                }
                AlterDlg.Show("保存成功");
                Addnew();
                Useful = FormUseful.新增;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("保存失败:"+ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void AssignmentTestDoc(TestData doc)
        {
            doc.DocNum = txtdanhao.Text;
            doc.LotNo = txtganghao.Text;
            doc.FabricName = txtpingming.Text;
            doc.Color = txtyanse.Text;
            doc.MoisturePermeability = txttoushi.Text;
            doc.Hydraulic = txtshuiya.Text;
            doc.Reqiured = txtzhibiao.Text;
            doc.RollType = txtgunxing.Text;
            doc.Divest = txtboli.Text;
            doc.CustomerName = txtkehu.Text;
            doc.DocumentDate = dateTimePicker1.Value;
            doc.OrderNum = txtordernum.Text;
        }
    }
}
